

cd $firmpath


global firstyear 1996 
global lastyear 2010


*** Prepare dataset
 
use panel_f2f_clean_19932017,clear
keep if year>=$firstyear & year<=$lastyear
rename numtvaanonym importer
cap rename mois month
rename kgs masse
rename unites usup
drop buyer
g iso2_tva=substr(importer,1,2)
replace iso2_tva="GR" if iso2_tva=="EL"
g dif=iso2!=iso2_tva 
tab dif 
save transaction_all$firstyear$lastyear, replace


* Input concordance (output of corres_nc8.do)
use $correspath/corres_nc8$firstyear$lastyear, clear
tostring nc8, replace
replace nc8="0"+nc8 if length(nc8)==7
sort nc8 year
save temp, replace

forvalues y=$firstyear(1)$lastyear{
	use temp, clear
	keep if year==`y'
	rename nc8 nc8_`y'
	sort id_conc
	save $tempath/corres_idconc_nc8`y', replace
}

* merge concordance with transaction dataset and output summary statistics on concorded products
use transaction_all$firstyear$lastyear, clear
sort nc8 year
merge m:m nc8 year using temp
tab nc8 if _merge==1
keep if _merge==3
drop _merge

sort siren importer iso2 year month id_conc 
collapse (sum) export   , by(siren importer iso2 year month id_conc  )

save transaction_all_id_conc$firstyear$lastyear, replace

use transaction_all_id_conc$firstyear$lastyear.dta, clear 
g trim=1 if month<4
replace trim=2 if month>=4 & month<7
replace trim=3 if month>=7 & month<10
replace trim=4 if month>=10
collapse (sum) export, by(year trim iso2 id_conc)
compress
save export9610, replace

use transaction_all_id_conc$firstyear$lastyear.dta, clear 
g trim=1 if month<4
replace trim=2 if month>=4 & month<7
replace trim=3 if month>=7 & month<10
replace trim=4 if month>=10
collapse (sum) export, by(year trim iso2 id_conc siren importer)
collapse (mean) mean=export, by(year trim iso2 id_conc)
merge 1:1  year trim iso2 id_conc using export9610
drop _m 
compress
save export9610, replace


* NEW MATCHES 

global firstyear 1996 // 2011 // 2011 
global lastyear 2010 // 2017

use transaction_all_id_conc$firstyear$lastyear.dta, clear 
sort year month 
egen date=group(year month) 
g buyer=importer
egen min=min(date), by(siren buyer iso2 id_conc) 
egen max=max(date), by(siren buyer iso2 id_conc) 

g new = date == min 
egen buyer_new=nvals(buyer) if new==1, by(year month id_conc iso2) 
egen seller_new=nvals(siren) if new==1, by(year month id_conc iso2) 

sort siren buyer iso2 id_conc date 
g die=siren!=siren[_n+1]| buyer!=buyer[_n+1] | id_conc!=id_conc[_n+1]
g length=max-min 
sum length, d 

g length_interv=0 
replace length_interv=1 if length <13 & length>0
replace length_interv=2 if length>12 & length < 37  
replace length_interv=3 if length>36 

collapse (sum) export new die (mean) length buyer_new seller_new, by(month year iso2 id_conc length_interv)
count 
compress
save series_newmatch$firstyear$lastyear, replace 

global end 2011
use $countrypath/WUI/WUI_EU, clear

* Adjust date format
g yearnum=substr(year,1,4)
destring yearnum, replace
g trim=substr(year,-1,1)
destring trim, replace
g date=yearnum+(trim-1)/4

keep if yearnum<$end 

g month=1 if trim==1
replace month=4 if trim==2
replace month=7 if trim==3
replace month=10 if trim==4
egen bom=bom(month yearnum) , f(%dm,_CY)

* identify large uncertainty episodes 
egen m=mean(WUI_)
egen sd=sd(WUI_)
egen max=max(WUI_)

g uncertainty= (WUI_> m + 1*sd) 
g uncertainty64= (WUI_> m + 1.64*sd) 

rename year yearstring
rename yearnum year
sort iso2 year trim
save $countrypath/uncertainty, replace
save temp, replace



use $correspath/corres_nc819962010.dta, clear // 19952010.dta, clear 
cap g nc8 = cn8
egen _=count(nc8), by(id_conc year) 
sort id_conc year
bys id_conc: egen nbm=max(_)
keep if year==2002 
tostring nc8, replace 
replace nc8="0"+nc8 if length(nc8)==7
g hs6_2002 =substr(nc8,1,6) if length(nc8)==8  

keep hs6 id_conc 
destring hs6, replace
merge m:1 hs6_2002 using $rspath\rs_hs02_19962006_controls 
collapse (mean) rs_hs6_b frac_lib_not_homog lup lsigma pci (max) dif_rauch , by(id_conc)
save temp2, replace 

use $countrypath/gdp_marketprice, clear 

g iso2="."
replace iso2="DE" if country=="Germany (until 1990 former territory of the FRG)"
replace iso2="AT" if country=="Austria"
replace iso2="BE" if country=="Belgium"
replace iso2="DK" if country=="Denmark"
replace iso2="ES" if country=="Spain"
replace iso2="FI" if country=="Finland"
replace iso2="GB" if country=="United Kingdom"
replace iso2="GR" if country=="Greece"
replace iso2="IT" if country=="Italy"
replace iso2="NL" if country=="Netherlands"
replace iso2="PT" if country=="Portugal"
replace iso2="SE" if country=="Sweden"

drop if iso2=="."

sort year trim 
egen t=group(year trim) 
egen c=group(iso2)
xtset c t 
destring gdp, replace
g gr_gdp=gdp/L4.gdp - 1
egen m=mean(gr_gdp), by(iso2) 
egen sd=sd(gr_gdp), by(iso2) 

g crisis=gr_gdp<m - sd 
drop c t date
destring year, replace 
save tmp4, replace


merge m:1 iso2 year trim using temp
keep if _m==3 
sort year trim 
egen t=group(year trim)
egen c=group(iso2)
xtset c t


reg gr_gdp WUI_ , ro

reg gr_gdp uncertainty L.uncertainty L2.uncertainty, ro
reghdfe gr_gdp uncertainty L.uncertainty L2.uncertainty , a(c) cluster(c)

reghdfe gr_gdp WUI_ , a(c) vce(ro)
reghdfe F.gr_gdp WUI_ , a(c) vce(ro)
reghdfe F2.gr_gdp WUI_ , a(c) vce(ro)
reghdfe F3.gr_gdp WUI_ , a(c) vce(ro)
reghdfe F4.gr_gdp WUI_ , a(c) vce(ro)





use series_newmatch$firstyear$lastyear, clear 
g trim=1 if month<4
replace trim=2 if month>=4 & month<7
replace trim=3 if month>=7 & month<10
replace trim=4 if month>=10
collapse (sum) new die export, by(year trim iso2 id_conc)
g insample=0
foreach i in $countrysample {
	replace insample=1 if iso2=="`i'"
}
keep if insample==1
sort iso2 id_conc year 
bys iso2 id_conc: g first=_n==1
g _=year if first==1
egen firsty=max(_), by(id_conc iso2)
fillin id_conc iso2 year trim 
replace export=0 if export==. 
sort iso2 id_conc year trim 
bys iso2 id_conc: g cum=sum(export) 

replace new=0 if new==. & cum>0 
replace die=0 if new==. & cum>0 

*sum nbm, d
*drop if nbm>6 // drop less than 1% of obs for which concordance might be an issue 


sort year trim 
merge m:1 iso2 year trim using temp 
keep if _m==3 
drop _m 

merge m:1 iso2 year trim using tmp4 
keep if _m==3 
drop _m 

merge m:1 id_conc using temp2 
keep if _m==3 
drop _m 

qui tab iso2, g(cty_)
egen cty=group(iso2) 

egen pm=group(id_conc trim) 
egen ct=group(iso2 trim year) 
egen pmt=group(id_conc trim year) 
egen pmct=group(id_conc trim year iso2)

g lnew=log(1+new) 
g ldie=log(die + 1) 

save base4reg_u$firstyear$lastyear, replace 

erase temp2.dta
erase tmp4.dta 
erase temp.dta 